import Vue from 'vue'
import App from './App'
import router from './router'
import 'element-ui/lib/theme-chalk/index.css'
import ElementUI from 'element-ui'
import store from './store';
import { actionRouter } from './router';

Vue.config.productionTip = false
Vue.use(ElementUI)

router.beforeEach((to, from, next) => {
    if (store.state.user_name) {
        if (store.state.newRouters.length !== 0) {
            next();
        } else {
            let newRouters
            if (store.state.role == 'A') {
                newRouters = actionRouter;
            } else {
                let children = actionRouter[0].children.filter(route => {
                    if (route.meta) {
                        if (route.meta.role == store.state.role) {
                            return true;
                        } else {
                            return false;
                        }
                    } else {
                        return true;
                    }
                });
                newRouters = actionRouter;
                newRouters[0].children = children;
            }
            router.addRoutes(newRouters);
            console.log(router);

            store.dispatch('setRoles', newRouters);
            console.log(to);

            next(to);
        }
    } else {
        to.path.includes('login') ? next() : next('/login');
    }
})
new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
})